﻿using CGT.OP.App_Start;
using CGT.OP.Common;
using CGT.OP.Models;
using OF.Component;
using OF.Component.BLL;
using OF.Component.DTO;
using OF.Component.Model;
using OF.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CGT.OP.Controllers
{
    public class BasicDataController : BaseController
    {
        #region 一级分类管理
        // GET: BasicData
        [Authentication(MenuCode = "cgt20101")]
        public ActionResult MasterBizType()
        {
            return View();
        }
        /// <summary>
        /// 一级分类
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ContentResult GetMasterBizTypeList()
        {
            ResultData<List<MasterBizTypeInfo>> rData = new ResultData<List<MasterBizTypeInfo>>();
            var list = MasterBizTypeOperate.QueryList(t => t.IsActive == 1);
            rData.data = list.ToList();
            return ReturnResult(rData);
        }


        /// <summary>
        /// 添加页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authentication(MenuCode = "cgt20101")]
        public ActionResult MasterBiztypeAdd()
        {
            //编辑传递id
            int id = EConvert.ToInt(GetParam("id"));
            MasterBizTypeInfo masterObj = null;
            if (id > 0)
            {
                masterObj = MasterBizTypeOperate.GetByID(id);
            }
            if (masterObj == null)
            {
                masterObj = new MasterBizTypeInfo();
            }
            return View(masterObj);
        }

        /// <summary>
        /// 保存一级分类
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ContentResult SaveMasterBizType(MasterBizTypeModel model)
        {
            ResultData<int> rData = new ResultData<int>();
            MasterBizTypeInfo info = new MasterBizTypeInfo();
            try
            {
                if (model.MasterBizTypeID < 1)
                {

                    info.MasterBizTypeName = model.MasterBizTypeName;
                    info.IsActive = 1;
                    info.CreatedDate = DateTime.Now;
                    info.MasterBizTypeID = MasterBizTypeOperate.GetMaxID();
                    info.CreatedUserID = CurrentUser().UserId;
                    info.UpdatedDate = DateTimeHelper.MinDateTime();
                    rData.data = MasterBizTypeOperate.Insert(info);
                }
                else
                {
                    info = MasterBizTypeOperate.GetByID(model.MasterBizTypeID);
                    info.MasterBizTypeName = model.MasterBizTypeName;
                    info.UpdatedDate = DateTime.Now;
                    info.UpdatedUserID = CurrentUser().UserId;
                    MasterBizTypeOperate.Update(info);
                    rData.data = model.MasterBizTypeID;
                }
            }
            catch (Exception ex)
            {
                rData.status = -1;
            }
            return ReturnResult(rData);
        }

        /// <summary>
        /// 删除一级分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ContentResult DelMasterBizType(int id)
        {
            ResultData<bool> rData = new ResultData<bool>();
            MasterBizTypeInfo masterObj = MasterBizTypeOperate.GetByID(id);
            if (masterObj != null)
            {
                masterObj.IsActive = 0;
                masterObj.UpdatedUserID = 1;
                masterObj.UpdatedDate = DateTime.Now;
                MasterBizTypeOperate.Update(masterObj);
                rData.data = true;
            }
            else
            {
                rData.status = -1;
                rData.data = false;
            }
            return ReturnResult(rData);
        }
        #endregion

        #region 二级分类管理
        [Authentication(MenuCode = "cgt20102")]
        public ActionResult BizType()
        {
            return View();
        }

        /// <summary>
        /// 二级分类
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ContentResult GetBizTypeList()
        {
            ResultData<List<BizTypeDto>> rData = new ResultData<List<BizTypeDto>>();
            BasePageConditionFront pageObj = new BasePageConditionFront();
            pageObj.rows = GetPageSize();
            pageObj.page = GetPageIndex();
            IList<BizTypeDto> list = BizTypeOperate.SearchList(pageObj);
            rData.data = list.ToList();
            rData.message = pageObj.total.ToString();
            return ReturnResult(rData);
        }


        /// <summary>
        /// 添加页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authentication(MenuCode = "cgt20102")]
        public ActionResult BizTypeAdd()
        {
            //编辑传递id
            int id = EConvert.ToInt(GetParam("id"));
            BizTypeInfo masterObj = null;
            if (id > 0)
            {
                masterObj = BizTypeOperate.GetByID(id);
            }
            if (masterObj == null)
            {
                masterObj = new BizTypeInfo();
            }
            return View(masterObj);
        }

        /// <summary>
        /// 保存二级分类
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ContentResult SaveBizType(BizTypeModel model)
        {
            ResultData<int> rData = new ResultData<int>();
            BizTypeInfo info = new BizTypeInfo();
            try
            {
                if (model.BizTypeID < 1)
                {

                    info.MasterBizTypeID = model.MasterBizTypeID;
                    info.IsActive = 1;
                    info.CreatedDate = DateTime.Now;
                    info.BizTypeID = BizTypeOperate.GetMaxID();
                    info.CreatedUserID = CurrentUser().UserId;
                    info.BizTypeName = model.BizTypeName;
                    info.BizTypeSort = model.BizTypeSort;
                    info.IsShowIndex = model.IsShowIndex;
                    info.BizTypeIcon = model.BizTypeIcon;
                    info.BizTypePinYin = model.BizTypePinYin;
                    info.UpdatedDate = DateTimeHelper.MinDateTime();
                    rData.data = BizTypeOperate.Insert(info);
                }
                else
                {
                    info = BizTypeOperate.GetByID(model.BizTypeID);
                    info.BizTypeName = model.BizTypeName;
                    info.UpdatedDate = DateTime.Now;
                    info.MasterBizTypeID = model.MasterBizTypeID;
                    info.BizTypeSort = model.BizTypeSort;
                    info.UpdatedUserID = CurrentUser().UserId;
                    info.IsShowIndex = model.IsShowIndex;
                    info.BizTypeIcon = model.BizTypeIcon;
                    info.BizTypePinYin = model.BizTypePinYin;
                    BizTypeOperate.Update(info);
                    rData.data = model.MasterBizTypeID;
                }
            }
            catch (Exception ex)
            {
                rData.status = -1;
            }
            return ReturnResult(rData);
        }

        /// <summary>
        /// 删除二级分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ContentResult DelBizType(int id)
        {
            ResultData<bool> rData = new ResultData<bool>();
            BizTypeInfo bizObj = BizTypeOperate.GetByID(id);
            if (bizObj != null)
            {
                bizObj.IsActive = 0;
                bizObj.UpdatedUserID = 1;
                bizObj.UpdatedDate = DateTime.Now;
                BizTypeOperate.Update(bizObj);
                rData.data = true;
            }
            else
            {
                rData.status = -1;
                rData.data = false;
            }
            return ReturnResult(rData);
        }

        /// <summary>
        /// 根据主分类ID获取二级分类
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ContentResult GetBizTypeListByMasterId(int masterId)
        {
            ResultData<List<BizTypeInfo>> rData = new ResultData<List<BizTypeInfo>>();
            var list = BizTypeOperate.QueryList(t => t.IsActive == 1 && t.MasterBizTypeID == masterId);
            rData.data = list.ToList();
            return ReturnResult(rData);
        }
        #endregion

        #region 办事指南
        [Authentication(MenuCode = "cgt2103")]
        public ActionResult GuidanceList()
        {
            return View();
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ContentResult GetGuidanceList()
        {
            ResultData<List<GuidanceDto>> rData = new ResultData<List<GuidanceDto>>();
            GuidanceCondition searchObj = new GuidanceCondition();
            searchObj.StartDateTime = "";
            searchObj.EndDateTime = "";
            searchObj.rows = GetPageSize();
            searchObj.page = GetPageIndex();
            int provinceId = EConvert.ToInt(GetParam("provinceid"));
            int cityId = EConvert.ToInt(GetParam("cityid"));
            int masterBizTypeId = EConvert.ToInt(GetParam("masterbiztype"));
            int bizTypeId = EConvert.ToInt(GetParam("biztype"));
            string sDate = GetParam("sDate");
            string eDate = GetParam("eDate");
            if (provinceId > 0)
            {
                searchObj.ProvinceId = provinceId;
            }
            if (cityId > 0)
            {
                searchObj.CityId = cityId;
            }
            if (masterBizTypeId > 0)
            {
                searchObj.MasterBizTypeId = masterBizTypeId;
            }
            if (bizTypeId > 0)
            {
                searchObj.BizTypeId = bizTypeId;
            }
            if (!string.IsNullOrWhiteSpace(sDate))
            {
                searchObj.StartDateTime = sDate;
            }
            if (!string.IsNullOrWhiteSpace(eDate))
            {
                searchObj.EndDateTime = eDate;
            }
            var list = GuidanceOperate.SearchList(searchObj);
            rData.data = list.ToList();
            rData.message = searchObj.total.ToString();
            return ReturnResult(rData);
        }

        /// <summary>
        /// 添加页面
        /// </summary>
        /// <returns></returns>
        [Authentication(MenuCode = "cgt2103")]
        public ActionResult GuidanceAdd()
        {
            int id = EConvert.ToInt(GetParam("id"));
            GuidanceDto obj = null;
            if (id > 0)
            {
                GuidanceCondition searchObj = new GuidanceCondition();
                searchObj.StartDateTime = "";
                searchObj.EndDateTime = "";
                searchObj.rows = 1;
                searchObj.page = 1;
                searchObj.GuidanceID = id;
                var list = GuidanceOperate.SearchList(searchObj);
                if (list.Count > 0)
                {
                    obj = list[0];
                }
            }
            if (obj == null)
            {
                obj = new GuidanceDto();
            }
            return View(obj);
        }

        /// <summary>
        /// 保存办事指南
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ContentResult SaveGuidance(GuidanceModel model)
        {
            ResultData<int> rData = new ResultData<int>();
            GuidanceInfo info = new GuidanceInfo();
            try
            {
                if (model.GuidanceID < 1)
                {

                    info.BizTypeID = model.BizTypeID;
                    info.IsActive = 1;
                    info.CreatedDate = DateTime.Now;
                    info.CreatedUserID = CurrentUser().UserId;
                    info.CityID = model.CityID;
                    info.GuidanceHtml = Ajax_decode(model.GuidanceHtml);
                    info.GuidanceWxMini = model.GuidanceWxMini;
                    info.UpdatedDate = DateTimeHelper.MinDateTime();
                    rData.data = GuidanceOperate.Insert(info);
                }
                else
                {
                    info = GuidanceOperate.GetByID(model.GuidanceID);
                    info.CityID = model.CityID;
                    info.UpdatedDate = DateTime.Now;
                    info.BizTypeID = model.BizTypeID;
                    info.GuidanceHtml = Ajax_decode(model.GuidanceHtml);
                    info.GuidanceWxMini = model.GuidanceWxMini;
                    info.UpdatedUserID = CurrentUser().UserId;
                    GuidanceOperate.Update(info);
                    rData.data = model.GuidanceID;
                }
            }
            catch (Exception ex)
            {
                rData.status = -1;
            }
            return ReturnResult(rData);
        }

        private string Ajax_decode(string str)
        {
            str = str.Replace("{$bai$}", "%");
            str = str.Replace("{$dan$}", "'");
            str = str.Replace("{$shuang$}", "\"");
            str = str.Replace("{$kong$}", " ");
            str = str.Replace("{$zuojian$}", "<");
            str = str.Replace("{$youjian$}", ">");
            str = str.Replace("{$and$}", "&");
            str = str.Replace("{$tab$}", "\t");
            str = str.Replace("{$jia$}", "+");
            str = str.Replace("'", "''");
            return str;
        }

        /// <summary>
        /// 删除指南
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ContentResult DelGuidanceById(int id)
        {
            ResultData<bool> rData = new ResultData<bool>();
            GuidanceInfo gObj = GuidanceOperate.GetByID(id);
            if (gObj != null)
            {
                gObj.IsActive = 0;
                gObj.UpdatedUserID = 1;
                gObj.UpdatedDate = DateTime.Now;
                GuidanceOperate.Update(gObj);
                rData.data = true;
            }
            else
            {
                rData.status = -1;
                rData.data = false;
            }
            return ReturnResult(rData);
        }
        #endregion

        #region 体检医院
        [Authentication(MenuCode = "cgt2105")]
        public ActionResult HospitalList()
        {
            return View();
        }

        public PartialViewResult HospitalPartialList(HospitalCondition dto)
        {
            dto.rows = GetPageSize();
            dto.page = GetPageIndex();
            IList<HospitalInfo> list = HospitalOperate.SearchList(dto);
            ViewBag.OutCount = dto.total;
            return PartialView(list);
        }
        [Authentication(MenuCode = "cgt2105")]
        public ActionResult HospitalEdit()
        {
            int id = EConvert.ToInt(GetParam("id"));
            var vm = new HospitalInfo();
            ViewBag.NameRead = "";
            if (id > 0)
            {
                ViewBag.NameRead = "readonly";
                vm = HospitalOperate.QuerySingle(x => x.HospitalID == id);
            }
            return View(vm);
        }
        public ContentResult HospitalSave(HospitalInfo vm)
        {
            ResultData<int> rData = new ResultData<int>();
            try
            {
                if (vm.HospitalID > 0)
                {
                    var entity = HospitalOperate.QuerySingle(x => x.HospitalID == vm.HospitalID);
                    entity.HospitalName = vm.HospitalName;
                    entity.ProvinceId = vm.ProvinceId;
                    entity.CityID = vm.CityID;
                    entity.Address = vm.Address;
                    entity.UpdatedDate = DateTime.Now;
                    entity.UpdatedUserID = this.CurrentUser().UserId;
                    entity.Initial = vm.Initial;
                    HospitalOperate.Update(entity);
                }
                else
                {
                    vm.CreatedDate = DateTime.Now;
                    vm.CreatedUserID = this.CurrentUser().UserId;
                    vm.IsActive = 1;
                    HospitalOperate.Insert(vm);
                }
            }
            catch (Exception ex)
            {
                rData.status = -1;
            }
            return ReturnResult(rData);
        }
        [HttpPost]
        public ContentResult HospitalLogicDelete()
        {
            ResultData<int> rData = new ResultData<int>();
            int id = EConvert.ToInt(GetParam("id"));
            try
            {
                var entity = HospitalOperate.QuerySingle(x => x.HospitalID == id);
                if (entity != null)
                {
                    entity.IsActive = 0;
                    HospitalOperate.Update(entity);
                }
                else
                {
                    rData.status = -1;
                }
            }
            catch (Exception ex)
            {
                rData.status = -1;
            }
            return ReturnResult(rData);
        }

        #endregion

        #region 快递公司管理
        [Authentication(MenuCode = "cgt2106")]
        public ActionResult ExpressList()
        {
            return View();
        }

        public PartialViewResult ExpressPartialList()
        {
            IList<ExpressCompanyInfo> list = ExpressCompanyOperate.QueryList(x => x.IsActive == 1);
            return PartialView(list);
        }

        [Authentication(MenuCode = "cgt2106")]
        public ActionResult ExpressEdit()
        {
            int id = EConvert.ToInt(GetParam("id"));
            var vm = new ExpressCompanyInfo();
            ViewBag.NameRead = "";
            if (id > 0)
            {
                ViewBag.NameRead = "readonly";
                vm = ExpressCompanyOperate.QuerySingle(x => x.IsActive == 1);
            }
            return View(vm);
        }

        public ContentResult ExpressSave(ExpressCompanyInfo vm)
        {
            ResultData<int> rData = new ResultData<int>();
            try
            {
                if (vm.ExpressCompanyID > 0)
                {
                    var entity = ExpressCompanyOperate.QuerySingle(x => x.ExpressCompanyID == vm.ExpressCompanyID);
                    entity.CompanyName = vm.CompanyName;
                    entity.Logo = vm.Logo;
                    entity.UpdatedDate = DateTime.Now;
                    entity.UpdatedUserID = this.CurrentUser().UserId;
                    ExpressCompanyOperate.Update(entity);
                }
                else
                {
                    vm.CreatedDate = DateTime.Now;
                    vm.CreatedUserID = this.CurrentUser().UserId;
                    vm.IsActive = 1;
                    ExpressCompanyOperate.Insert(vm);
                }
            }
            catch (Exception ex)
            {
                rData.status = -1;
            }
            return ReturnResult(rData);
        }

        [HttpPost]
        public ContentResult ExpressLogicDelete()
        {
            ResultData<int> rData = new ResultData<int>();
            int id = EConvert.ToInt(GetParam("id"));
            try
            {
                var entity = ExpressCompanyOperate.QuerySingle(x => x.ExpressCompanyID == id);
                if (entity != null)
                {
                    entity.IsActive = 0;
                    ExpressCompanyOperate.Update(entity);
                }
                else
                {
                    rData.status = -1;
                }
            }
            catch (Exception ex)
            {
                rData.status = -1;
            }
            return ReturnResult(rData);
        }

        [HttpPost]
        public ContentResult UploadImg()
        {
            ResultData<string> rData = new ResultData<string>();

            if (Request.Files.Count > 0)
            {
                try
                {
                    HttpPostedFileBase f = Request.Files["txt_file"];
                    var path = ConfigHelper.GetValue("ExpressImagePath");
                    f.SaveAs(path + f.FileName);
                    rData.data = f.FileName;
                }
                catch (Exception ex)
                {
                    rData.message = "上传失败";
                }
            }

            return ReturnResult(rData);
        }
        #endregion
    }
}